/**
 * Copyright (C), 2007-2009, 
 * File Name: page_user.js
 * author:胡作正
 * Encoding: UTF-8
 * Version: 1.0
 * Date: February 2, 2010
 * History:	
 */
 
Ext.namespace("Ext.milan.pagejs.page_user");


Ext.milan.pagejs.page_user.userWin = null;
Ext.milan.pagejs.page_user.subWin = null;
Ext.milan.pagejs.page_user.REQUEST_ACTION = "user";
Ext.milan.pagejs.page_user.ORG_TREE_ID = "select_pos_tree";
var selectPostionTree = null;
Ext.milan.pagejs.page_user.docLoadBeforeCallBack = function() {
};

Ext.milan.pagejs.page_user.docOnReadyCallback = function() {

};

/**
 * 关闭子弹出窗口
 */
Ext.milan.pagejs.page_user.closeSubWin = function() {
	Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_user.subWin);
};

/**
 * 关闭弹出窗口
 */
Ext.milan.pagejs.page_user.closeWin = function(winName) {
	if(winName){
		if(winName=='inactivateConsignerWin')
			inactivateConsignerWin.closeDialog();
	}
	else
		Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_user.userWin);
};

/*------------------------ private methods -----------------------*/

/**
 * 查询所有用户
 */
Ext.milan.pagejs.page_user._search = function() {
	Ext.milan.pagejs.common_utils.query(false, "user/list.do");
};

/**
 * 新建/编辑用户表单验证
 * 
 * @param frm 表单元素
 */
Ext.milan.pagejs.page_user._formCheck = function(frm) {
	function focusElement(el) {
		if(el.focus)
			el.focus();
	};
	
	if(frm.username.value.trim() == "") {
		new Milan.widget.Dialog("提示信息", "用户名 不能为空！", 
			{confirmCallback: function(){focusElement(frm.username);}});
		return false;
	}

	var re = /^[a-zA-Z]{0,1}[a-zA-Z0-9_\.]{0,29}$/;
	if (!re.test(frm.username.value)) {
		new Milan.widget.Dialog("提示信息", "用户名只允许为英文字母、数字、下划线！", 
			{confirmCallback: function(){focusElement(frm.username);}});
		return false;
	}
	
	if(frm.realname.value.trim() == "") {
		new Milan.widget.Dialog("提示信息", "真实姓名 不能为空！", 
			{confirmCallback: function(){focusElement(frm.realname);}});
		return false;
	}
	
	//之所以允许姓名包含数字，是因为可能多人存在相同姓名时，常常通过数字以示区别，如刘星，刘星2
	var pattern = /^[a-zA-Z0-9\u4e00-\u9fa5\s]{0,20}$/;
	if (!pattern.test(frm.realname.value)) {
		new Milan.widget.Dialog("提示信息", "真实姓名只允许为字母、数字、空格、中文！", 
			{confirmCallback: function(){focusElement(frm.realname);}});
		return false;
	}

	if(frm.pwd.value != "") {
		var reg = /^[a-zA-Z0-9~`!@#\$\^&\*\(\)\-_\+\|\?\.><,:;\{\}\[\]\/"']{6,20}$/;
		if(!reg.test(frm.pwd.value)) {
	    	new Milan.widget.Dialog("提示信息", "密码不允许包含空格。且长度必须在6-20之间！", 
	    		{confirmCallback: function(){focusElement(frm.pwd);}});
			return false;
		}
		
		if (frm.pwd.value.trim() != frm.repwd.value.trim()) {
			new Milan.widget.Dialog("提示信息", "密码确认错误！", 
				{confirmCallback: function(){focusElement(frm.repwd);}});
			return false;
		}
	}
	
	if(!frm.memo.value.isEmpty() && frm.memo.value.length > 100) {
	    new Milan.widget.Dialog("提示信息", "备注长度不能大于100个字符！", 
	    	{confirmCallback: function(){focusElement(frm.memo);}});
		return false;
	}

	return true;
};

Ext.milan.pagejs.page_user._formCheckAdmin = function(frm) {
	function focusElement(el) {
		if(el.focus)
			el.focus();
	};
	
	if(frm.did.value.trim() == "") {
		new Milan.widget.Dialog("提示信息", "所属客户 不能为空！", 
			{confirmCallback: function(){}});
		return false;
	}
	
	if(frm.category.value.trim() == "") {
		new Milan.widget.Dialog("提示信息", "用户类别 不能为空！", 
			{confirmCallback: function(){}});
		return false;
	}
	return true;
};

/**
 * 保存用户信息
 */
Ext.milan.pagejs.page_user._saveInfo = function(formId) {
	if(!Ext.milan.pagejs.page_user._formCheck($(formId)))
		return;
		
	$$("#position_selected_list option").each(function(ele){ele.selected=true;});
	$$("#selected_privilege_group_list option").each(function(ele){ele.selected=true;});
	Milan.ajaxutility.formSubmit({
		form:formId,
		success:function(response)
		{
			Ext.milan.pagejs.page_user.closeWin();
			Ext.milan.pagejs.page_user.query();
		}
	});
};

/*------------------------ public methods -----------------------*/

/**
 * 条件查询用户列表
 */
Ext.milan.pagejs.page_user.query = function() {
	Milan.pagejs.page_index.updateCurrentTabByForm("query_user_list_form");
};

/**
 * 显示新建用户表单
 */
Ext.milan.pagejs.page_user.showAddUserForm = function() {
	var url = Ext.milan.pagejs.page_user.REQUEST_ACTION + "/create.do";
	Ext.milan.pagejs.page_user.userWin = Milan.commonutils.createModelPop({
		title:"用户管理", 
		url:url,
		width:650
	});
};

/**
 * 保存新建用户信息
 */
Ext.milan.pagejs.page_user.addUser = function(formid) {
	Ext.milan.pagejs.page_user._saveInfo(formid);
};


/**
 * 显示编辑用户表单
 * 
 * @param userId 用户ID
 */
Ext.milan.pagejs.page_user.showEditUserForm = function(userId) {
	var url = Ext.milan.pagejs.page_user.REQUEST_ACTION + "/edit.do?userid=" + userId;
	Ext.milan.pagejs.page_user.userWin = Milan.commonutils.createModelPop({
		title:"编辑用户",
		url:url,
		width:650
	});
};
/**
 * 保存编辑后的用户信息
 */
Ext.milan.pagejs.page_user.editUser = function() {
	Ext.milan.pagejs.page_user._saveInfo("edit_user_info_form");
};

/**
 * 删除用户
 * 
 * @param userId
 */
Ext.milan.pagejs.page_user.deleteUser = function(userId) {
	var url = "user/delete.do?userid=" + userId;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该用户吗？", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url: url,
				method: 'post',
				success: function(response, option)
				{
					Ext.milan.pagejs.page_user.query();
				}
			});
		}
	});	
};
/**
 * 删除用户
 * 
 * @param userId
 */
Ext.milan.pagejs.page_user.deleteAdminUser = function(userId) {
	var url = "user/delete.do?userid=" + userId;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该用户吗？", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url: url,
				method: 'post',
				success: function(response, option)
				{
					Ext.milan.pagejs.page_user.queryAdmin();
				}
			});
		}
	});	
};

Ext.milan.pagejs.page_user.deleteBaseUser = function(userId) {
	var url = "user/delete.do?userid=" + userId;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该用户吗？", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url: url,
				method: 'post',
				success: function(response, option)
				{
					Ext.milan.pagejs.page_user.queryBase();
				}
			});
		}
	});	
};

Ext.milan.pagejs.page_user.inactivateUsers = function()
{
	if (CheckboxSelectUtils.calcuCheckedboxes($$("#query_user_list_form input[name='items']"))==0)
	{
		new Milan.widget.Dialog('提示信息', "必须先选择用户");
		return;
	}
	new Milan.widget.ConfirmDialog("确认信息", "确认禁用这些用户吗？<br>提示：禁用后用户将无法登录！您可以使用启用功能来重新启用", {
		confirmCallback: function() {		
			$("query_user_list_form").action = "user/batchDisableActive.do";
			Milan.ajaxutility.formSubmit({
				method: 'post',
				form:'query_user_list_form',
				success: function(response, option)
				{
					$("query_user_list_form").action = "user/list.do";
					Ext.milan.pagejs.page_user.query();
				}
			});
		}
	});	
};

Ext.milan.pagejs.page_user.activiseUsers = function()
{
	if (CheckboxSelectUtils.calcuCheckedboxes($$("#query_user_list_form input[name='items']"))==0)
	{
		Milan.commonutils.createDelayNotifyModel('提示信息', "必须先选择用户");
		return;
	}
	new Milan.widget.ConfirmDialog("确认信息", "确认要启用选中的用户吗？", {
		confirmCallback: function() {
			$("query_user_list_form").action = "user/batchStartActive.do";
			Milan.ajaxutility.formSubmit({
				method: 'post',
				form:'query_user_list_form',
				success: function(response, option)
				{
					$("query_user_list_form").action = "user/list.do";
					Ext.milan.pagejs.page_user.query();
				}
			});
		}
	});
};

/**
 * 重置用户密码
 * 
 * @param userId
 * @param userAccount
 */
Ext.milan.pagejs.page_user.resetUserPwd = function(userId, userAccount) {
	var url = "user/?userid="+ userId + "&account=" + userAccount;
	new Milan.widget.ConfirmDialog("确认信息", "确认要重置该用户的密码吗？重置密码将在用户确认后生效。", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url:url
			});
		}
	});	
};

/**
 * 查看详情
 * 
 * @param entityId 用户ID
 */
Ext.milan.pagejs.page_user.showDetail = function(entityId) {
	var url = Ext.milan.pagejs.page_user.REQUEST_ACTION + "/show.do?userid=" + entityId;
	Ext.milan.pagejs.page_user.userWin = Milan.commonutils.createModelPop({
		title:"用户详细信息",
		url:url,
		width:500
	});
};

/**
 * 查看用户档案
 * 
 * @param entityId 用户ID
 */
Ext.milan.pagejs.page_user.showUserProfile = function(entityId) {
	var url = "userProfileAction.do?actionType=10&fromUserDetail=1&entityId=" + entityId;
	Ext.milan.pagejs.page_user.subWin = Milan.commonutils.createModelPop({
		title:"用户详细信息",
		url:url,
		width:750
	});
};



/**
 * 显示“选择权限组”弹出框
 */
Ext.milan.pagejs.page_user.showSelectPrivilegeGroupWin = function() {
	var url = "group/selectGroup.do";
	Ext.milan.pagejs.page_user.subWin = Milan.commonutils.createModelPop({
		title:"选择权限组",
		url:url,
		width:260
	});
};

Ext.milan.pagejs.page_user.selectPrivilegeConfig = function() {
	var selectedConfigs = document.getElementsByName("configIds");
	for(var i = 0,ilen=selectedConfigs.length; i < ilen; i++) {
		var chkObj = selectedConfigs[i];
		if(chkObj.checked) {
			document.getElementById('_ctgname_of_add_wf').innerHTML = chkObj.title;
			$('did').value = chkObj.value;
			Ext.milan.pagejs.page_user.closeSubWin();
		}
	}
};


/**
 * 移出所有条目
 * 
 * @param btn 操作按钮
 * @param cls
 */
Ext.milan.pagejs.page_user.removeAllItems = function(selectId) {
	var selectObj = $(selectId);
	var selectOptions = selectObj.options;
	if (selectOptions && selectOptions.length != 0){
		var ilen=selectOptions.length;
		for (var i=ilen-1; i>=0; i--)
		{
			selectObj.remove(i);
		}
	}
	Ext.milan.pagejs.page_user.hideBtn(selectId);
};
//隐藏按钮
Ext.milan.pagejs.page_user.hideBtn=function(id) {
	if(id == "position_selected_list"){
	    Element.hide("position_selected_list");
		Element.hide("remove_user_pos_btn");
		Element.hide("remove_all_user_pos_btn");
	}else {
		Element.hide("remove_all_privilege_group_btn");
		Element.hide("selected_privilege_group_list");
		Element.hide("remove_privilege_group_btn");
	}
};
/**
 * 移出所选择的条目
 * 
 * @param btn
 * @param formId
 * @param elementName
 */
Ext.milan.pagejs.page_user.removeSelectedItems = function(selectId) {
	var selectObj = $(selectId);
	var selectOptions = selectObj.options;
	if (selectOptions == null || selectOptions.length == 0 || selectOptions.selectedIndex == -1)
	{
		var msg="请先选择要删除的权限组！";
		if(selectId=="position_selected_list")msg="请先选择要删除的岗位！";
		new Milan.widget.Dialog("提示信息",msg);
		return;
	}
	var ilen=selectOptions.length;
	for (var i=ilen-1; i>=0; i--)
	{
		if (selectObj[i].selected)
		    selectObj.remove(i);
	}
	if (selectOptions == null || selectOptions.length == 0)
	{	
		Ext.milan.pagejs.page_user.hideBtn(selectId);
	}	
};

/**
 * 显示“选择部门”弹出框
 */
Ext.milan.pagejs.page_user.showSelectDeptWin = function(formId) {
	new Milan.widget.Selector({
		orgType:"32",
		title:"部门选择",
		deptUrl:"dept/selectDept.do?",
		quickSelectUrl:"dept/quickSelectDept.do?",
		searchUrl:"dept/selectDeptByDeptid.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false
	},
	false,
	{
		onSelectedCallBack : function(arr)
		{
	 		if (arr!=null)
	    	{
			    var form = $(formId);
			    document.getElementById('_dept_of_add_wf').innerHTML = arr[0].name;
		        form.deptid.value = arr[0].deptid;
			}
		}
	});
};

Ext.milan.pagejs.page_user.removeSelectDeptWin = function(formId){
	var form = $(formId);
    document.getElementById('_dept_of_add_wf').innerHTML = '';
    form.deptid.value = '';
}

/**
 * 显示“选择部门”弹出框
 */
Ext.milan.pagejs.page_user.showSelectTaskDeptWin = function(formId) {
	new Milan.widget.Selector({
		orgType:"32",
		title:"部门选择",
		deptUrl:"dept/selectDept.do?",
		quickSelectUrl:"dept/quickSelectDept.do?",
		searchUrl:"dept/selectDeptByDeptid.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false
	},
	false,
	{
		onSelectedCallBack : function(arr)
		{
	 		if (arr!=null)
	    	{
			    var form = $(formId);
			    document.getElementById('_dept_of_add_task').innerHTML = arr[0].name;
		        form.taskdeptid.value = arr[0].deptid;
			}
		}
	});
};
Ext.milan.pagejs.page_user.removeSelectTaskDeptWin = function(formId){
	var form = $(formId);
    document.getElementById('_dept_of_add_task').innerHTML = '';
    form.taskdeptid.value = '';
}


/**
 * 选择权限组
 */	
Ext.milan.pagejs.page_user.selectPrivilegeGroup = function() {
	var selectedGroups = document.getElementsByName("groupIds");
	var privilegeGroupContaniner = $("selected_privilege_group_list");
	var ownerOptions=privilegeGroupContaniner.options
	var strOwnerOptions=",";
	var controlledGroups = [];
	var chkObj = null;
	var nothing=true;//判断是否有选择权限组
	
	//由于“移除”和“移除全部”按钮的显示隐藏是一体的，所以只需用一个来判断状态
	var hasDisplayed = ($("remove_privilege_group_btn").style.display == "");
	if(ownerOptions&&ownerOptions.length>0){
		for(var ii=0,iilen=ownerOptions.length;ii<iilen;ii++)
			strOwnerOptions=strOwnerOptions + ownerOptions[ii].value + ",";
	}
	for(var i = 0,ilen=selectedGroups.length; i < ilen; i++) {
		chkObj = selectedGroups[i];
		if(chkObj.checked) {
			nothing=false;
			var level = 0;
			if(chkObj.getAttribute('level') == "true") {
				level = 1;
				controlledGroups.push(chkObj.title);
			}
			if(strOwnerOptions.indexOf(","+chkObj.value + "|" + level+",")>-1)continue;
			var oOption = new Option(chkObj.title,chkObj.value + "|" + level);
			privilegeGroupContaniner.options.add(oOption);
			if(!hasDisplayed) {
				if($("remove_privilege_group_btn").style.display == "none")
					Element.show("remove_privilege_group_btn");
				
				if($("remove_all_privilege_group_btn").style.display == "none")
					Element.show("remove_all_privilege_group_btn");
				
				hasDisplayed = true;
			}
		}
		if(!nothing)Element.show("selected_privilege_group_list");
	}
	
	if(controlledGroups.length > 0) {
		var controlledGroupNames = "";
		for(var j = 0,jlen=controlledGroups.length; j < jlen; j++) {
			controlledGroupNames += "[" + controlledGroups[j] + "]";
		}
		
		Ext.milan.pagejs.page_user.closeSubWin();
	}else {
		Ext.milan.pagejs.page_user.closeSubWin();
	}
};

/**
 * 全选操作
 * 
 * @param checkState 具有全选功能的那个复选框的选中状态
 */	
Ext.milan.pagejs.page_user.selectAll = function(checkState) {
	var checkboxEls = document.getElementsByName("groupIds");
	CheckboxSelectUtils.selectAll(checkState, checkboxEls);
};

/**
 * 显示设置用户权限页面
 */
Ext.milan.pagejs.page_user.showSetPermissionPage = function(userID, userName)
{
    var url = "user/checkPer.do?userid=" + userID;
	Milan.pagejs.page_index.updateCurrentTabByUrl(url);
}

/**
 * 查看用户的最终权限
 */
Ext.milan.pagejs.page_user.viewUserFinalPermission = function(userID, userName)
{
    var url = "user/showPer.do?userid=" + userID;
    Milan.pagejs.page_index.updateCurrentTabByUrl(url);
}

Ext.milan.pagejs.page_user.queryDepartmentLoginSum = function() {
	var form = $("_frm_mng_dept_login_sum");
	if (form.queryStartDateByQuery.value.trim()!="" && form.queryEndDateByQuery.value.trim()=="")
	{
        new Milan.widget.Dialog("输入提示", "截止日期必须填写！");
        return;
	}
	if (form.queryEndDateByQuery.value.trim()!="" && form.queryStartDateByQuery.value.trim()=="")
	{
        new Milan.widget.Dialog("输入提示", "起始日期必须填写！");
        return;
	}
	 if(form.queryStartDateByQuery.value>form.queryEndDateByQuery.value)
	 {
		 new Milan.widget.Dialog('提示信息',"起始日期不得大于截止日期!");
		 return ;
	 }
      
	form.actionType.value = 51;
	Ext.milan.extutils.ajaxutility.formSubmit(
	{
		form : "_frm_mng_dept_login_sum",
		success : function(response, option)
		{
			Milan.pagejs.page_index.updateCurrentTab(response.responseText);
		}
	});
}

Ext.milan.pagejs.page_user.exportDepartmentLoginSum = function() {
	var form = $("_frm_mng_dept_login_sum");
	if (form.queryStartDateByQuery.value.trim()!="" && form.queryEndDateByQuery.value.trim()=="")
	{
        new Milan.widget.Dialog("输入提示", "截止日期必须填写！");
        return;
	}
	if (form.queryEndDateByQuery.value.trim()!="" && form.queryStartDateByQuery.value.trim()=="")
	{
        new Milan.widget.Dialog("输入提示", "起始日期必须填写！");
        return;
	}
    Ext.milan.extutils.commonutils.downloadFile(
    {
        url: "userAction.do?actionType=51&isExport=1&queryStartDateByQuery=" + form.queryStartDateByQuery.value + "&queryEndDateByQuery=" + form.queryEndDateByQuery.value
    });
}

Ext.milan.pagejs.page_user.queryDepartmentUserLoginLog = function(deptId) {
	var form = $("_frm_mng_dept_login_sum");
	Milan.pagejs.page_index.linkclick("部门用户登录记录", "userAction.do?actionType=52&queryStartDateByQuery=" + form.queryStartDateByQuery.value + "&queryEndDateByQuery=" + form.queryEndDateByQuery.value + "&deptId=" + deptId);
}

Ext.milan.pagejs.page_user.refreshDepartmentUserSum = function() {
	var form = $("_frm_mng_dept_login_sum");
	form.actionType.value = 53;
	Ext.milan.extutils.ajaxutility.formSubmit(
	{
		form : "_frm_mng_dept_login_sum",
		success : function(response, option)
		{
		}
	});
}


/**
 * 显示用户信息（包含基本信息和详细信息）
 */
Ext.milan.pagejs.page_user.viewUserDetail = function(userId,popTitle) 
{
	var url = "user/view.do?userid=" + userId;
	Ext.milan.pagejs.page_user.userWin = Milan.commonutils.createModelPop({
		title:"人员信息", 
		url:url,
		height: "300px",
		width : "500px"
	});
}
Ext.milan.pagejs.page_user.directoryPermission = function(userId,username){
	var url = "directory/auth.do?authid=" + userId+"&authType=1&dirType=2";
	win = Milan.commonutils.createModelPop({
		title:"目录权限",
		url:url,
		height: "600px",
		width : "320px"
	});
}